package com.sortingalgorithm;

import java.util.Arrays;

/*
* 选择排序
* */
public class SelectSort {
    public static void main(String[] args) {
        int[] arr = {1,3,6,7,2,54,54,4};
        selectSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void selectSort(int[] arr){
        if(arr == null || arr.length < 2){
            return;
        }
        for(int i = 0; i < arr.length - 1;i++){
            int pivot = arr[i];
            int start = i;
            int end = i;
            for(int j = i + 1;j < arr.length;j++){
                if(arr[j] < pivot){
                    pivot = arr[j];
                    end = j;
                }
            }
            swap(arr,start,end);
        }
    }

    private static void swap(int[] arr,int i,int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
